<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: CommentDAO.cfc 579 2006-05-16 19:42:44Z mpwoodward $

Notes:
--->
<cfcomponent 
	displayname="CommentDAO" 
	output="false" 
	extends="CommentDAO" 
	hint="CommentDAO for MachBlog (MSSQL)">
	
	<cffunction name="init" access="public" output="false" returntype="CommentDAO" 
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="create" access="public" output="false" returntype="void" hint="Creates a new comment">
		<cfargument name="comment" type="Comment" required="true" />
		
		<cfset var insertComment = "" />
		
		<cftry>
			<cfquery name="insertComment" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				INSERT INTO machblog_comment (
					comment_id, 
					entry_id, 
				<cfif arguments.comment.getName() NEQ "">
					name, 
				</cfif>
				<cfif arguments.comment.getEmail() NEQ "">
					email, 
				</cfif>
				<cfif arguments.comment.getUrl() NEQ "">
					url, 
				</cfif>
				<cfif arguments.comment.getComment() NEQ "">
					comment, 
				</cfif>
				<cfif arguments.comment.getAudit().getCreatedById() NEQ "">
					created_by_id, 
				</cfif>
					dt_created, 
					ip_created, 
					is_active, 
					is_subscribed, 
					subscription_only,
					subscriber_email_sent
				) VALUES (
					<cfqueryparam value="#arguments.comment.getCommentId()#" cfsqltype="cf_sql_char" 
							maxlength="35" />, 
					<cfqueryparam value="#arguments.comment.getEntryId()#" cfsqltype="cf_sql_char" 
							maxlength="35" />, 
				<cfif arguments.comment.getName() NEQ "">
					<cfqueryparam value="#arguments.comment.getName()#" cfsqltype="cf_sql_varchar" 
							maxlength="100" />, 
				</cfif>
				<cfif arguments.comment.getEmail() NEQ "">
					<cfqueryparam value="#arguments.comment.getEmail()#" cfsqltype="cf_sql_varchar" 
							maxlength="100" />, 
				</cfif>
				<cfif arguments.comment.getUrl() NEQ "">
					<cfqueryparam value="#arguments.comment.getUrl()#" cfsqltype="cf_sql_varchar" 
							maxlength="250" />, 
				</cfif>
				<cfif arguments.comment.getComment() NEQ "">
					<cfqueryparam value="#arguments.comment.getComment()#" 
									cfsqltype="cf_sql_longvarchar" />, 
				</cfif>
				<cfif arguments.comment.getAudit().getCreatedById() NEQ "">
					<cfqueryparam value="#arguments.comment.getAudit().getCreatedById()#" 
									cfsqltype="cf_sql_char" maxlength="35" />, 
				</cfif>
					<cfqueryparam value="#arguments.comment.getAudit().getDtCreated()#" cfsqltype="cf_sql_bigint" />, 
					<cfqueryparam value="#arguments.comment.getAudit().getIpCreated()#" 
								cfsqltype="cf_sql_varchar" maxlength="15" />, 
					<cfqueryparam value="#arguments.comment.getAudit().getIsActive()#" cfsqltype="cf_sql_bit" />, 
					<cfqueryparam value="#arguments.comment.getIsSubscribed()#" cfsqltype="cf_sql_bit" />, 
					<cfqueryparam value="#arguments.comment.getSubscriptionOnly()#" cfsqltype="cf_sql_bit" />,
					<cfqueryparam value="0" cfsqltype="cf_sql_bit" />
				)
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
	</cffunction>
	
	<cffunction name="read" access="public" output="false" returntype="void" hint="Populates a Comment bean">
		<cfargument name="comment" type="Comment" required="true" />
		
		<cfset var getComment = "" />
		<cfset var cbName = "" />
		<cfset var mbName = "" />
		<cfset var dtModified = 0 />
		<cfset var subscriptionOnly = false />
		<cfset var subscriberEmailSent = false />
		
		<cftry>
			<cfquery name="getComment" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT 	machblog_comment.*, 
						cb.first_name AS cbfn, 
						cb.last_name AS cbln, 
						mb.first_name AS mbfn, 
						mb.last_name AS mbln 
				FROM 	machblog_comment 
				LEFT OUTER JOIN machblog_user AS cb ON machblog_comment.created_by_id = cb.user_id 
				LEFT OUTER JOIN machblog_user AS mb ON machblog_comment.modified_by_id = mb.user_id 
				WHERE machblog_comment.comment_id = <cfqueryparam value="#arguments.comment.getCommentId()#" 
															cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error [databaseerror]" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
		
		<cfscript>
			// if we got a hit, populate the object
			if (IsQuery(getComment) AND getComment.RecordCount EQ 1) {
				if (getComment.created_by_id NEQ "") {
					cbName = getComment.cbfn & " " & getComment.cbln;
				} else {
					cbName = getComment.name;
				}
				
				if (getComment.modified_by_id NEQ "") {
					mbName = getComment.mbfn & " " & getComment.mbln;
					dtModified = getComment.dt_modified;
				}
				
				if (getComment.subscriber_email_sent NEQ "") {
					subscriberEmailSent = getComment.subscriber_email_sent;
				}
				
				if (getComment.subscription_only NEQ "") {
					subscriptionOnly = getComment.subscription_only;
				}
				
				arguments.comment.init(getComment.comment_id, getComment.entry_id, cbName, 
										getComment.email, getComment.url, getComment.comment, 
										getComment.is_subscribed, subscriptionOnly, subscriberEmailSent);
				arguments.comment.getAudit().init(getComment.created_by_id, cbName, 
													getComment.dt_created, getComment.ip_created, 
													getComment.modified_by_id, mbName, 
													dtModified, getComment.ip_modified, 
													getComment.is_active);
			}
		</cfscript>
	</cffunction>
	
	<cffunction name="update" access="public" output="false" returntype="void" hint="Updates a comment">
		<cfargument name="comment" type="Comment" required="true" />
		
		<cfset var updateComment = "" />
		
		<cftry>
			<cfquery name="updateComment" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				UPDATE 	machblog_comment 
				SET 	entry_id = <cfqueryparam value="#arguments.comment.getEntryId()#" 
														cfsqltype="cf_sql_char" maxlength="35" />, 
					<cfif arguments.comment.getName() NEQ "">
						name = <cfqueryparam value="#arguments.comment.getName()#" 
												cfsqltype="cf_sql_varchar" maxlength="100" />, 
					<cfelse>
						name = null, 
					</cfif>
					<cfif arguments.comment.getEmail() NEQ "">
						email = <cfqueryparam value="#arguments.comment.getEmail()#" 
												cfsqltype="cf_sql_varchar" maxlength="100" />, 
					<cfelse>
						email = null, 
					</cfif>
					<cfif arguments.comment.getUrl() NEQ "">
						url = <cfqueryparam value="#arguments.comment.getUrl()#" 
												cfsqltype="cf_sql_varchar" maxlength="250" />,
					<cfelse>
						url = null,  
					</cfif>
					<cfif arguments.comment.getComment() NEQ "">
						comment = <cfqueryparam value="#arguments.comment.getComment()#" 
												cfsqltype="cf_sql_longvarchar" />, 
					<cfelse>
						comment = null, 
					</cfif>
					<cfif arguments.comment.getAudit().getModifiedById() NEQ "">
						modified_by_id = <cfqueryparam value="#arguments.comment.getAudit().getModifiedById()#" 
														cfsqltype="cf_sql_char" maxlength="35" />, 
					</cfif>
						dt_modified = <cfqueryparam value="#arguments.comment.getAudit().getDtModified()#" 
													cfsqltype="cf_sql_bigint" />, 
						ip_modified = <cfqueryparam value="#arguments.comment.getAudit().getIpModified()#" 
													cfsqltype="cf_sql_varchar" maxlength="15" />, 
						is_active = <cfqueryparam value="#arguments.comment.getAudit().getIsActive()#" 
													cfsqltype="cf_sql_bit" />, 
						is_subscribed = <cfqueryparam value="#arguments.comment.getIsSubscribed()#" 
														cfsqltype="cf_sql_bit" />, 
						subscription_only = <cfqueryparam value="#arguments.comment.getSubscriptionOnly()#" 
														cfsqltype="cf_sql_bit" />, 
						subscriber_email_sent = <cfqueryparam value="#arguments.comment.getSubscriberEmailSent()#" 
														cfsqltype="cf_sql_bit" /> 
				WHERE 	comment_id = <cfqueryparam value="#arguments.comment.getCommentId()#" 
													cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
	</cffunction>
	
	<cffunction name="delete" access="public" output="false" returntype="void" hint="Deletes a comment">
		<cfargument name="comment" type="Comment" required="true" />
		
		<cfset var deleteComment = "" />
		
		<cftry>
			<cfquery name="deleteComment" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" 
					password="#getDatasource().getPassword()#">
				DELETE FROM machblog_comment 
				WHERE comment_id = <cfqueryparam value="#arguments.comment.getCommentId()#" 
													cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
	</cffunction>
</cfcomponent>
